from selenium import webdriver
from selenium.common.exceptions import InvalidArgumentException, WebDriverException, NoSuchElementException
from selenium.webdriver import Keys
from selenium.webdriver.common.by import By
#打开浏览器
class Browser:

    def browser(self, browser1='chrome'):
        if browser1 == 'chrome':
            self.driver=webdriver.Chrome()
        elif browser1 == 'firefox':
            self.driver= webdriver.Firefox()
        elif browser1 == 'edge':
            self.driver =webdriver.Edge()
        else:
            print('不支持该浏览器')
            self.driver = None

# 进入网址

    def address(self,tion='http://www.baidu.com/'):
        try:
            return self.driver.get(tion)
        except (InvalidArgumentException, WebDriverException):
            print('地址输入不正确')

# 定位标签
    def location(self, label):
        try:
            return self.driver.find_element(*label)
        except NoSuchElementException:
            print('定位失败')
    # 输入框
    def input(self,label):
        i=self.location(label)
        if i is not None:
            i.send_keys(input)
    def delete_cookie(self):
        self.driver.delete_cookie()

    def clavier_operation(self, locator, clavier):
        # TODO 键盘操作
        element = self.location(locator)
        if clavier == '全选':
            element.send_keys(Keys.CONTROL, 'a')
        elif clavier == '复制':
            element.send_keys(Keys.CONTROL, 'c')
        elif clavier == '粘贴':
            element.send_keys(Keys.CONTROL, 'v')

    # 点击
    def click(self, label):
        element = self.location(label)
        if element is not None:
            element.click()
    #退出浏览器
    def quit(self):
        self.driver.quit()

if __name__ == '__main__':
    csae =Browser()
    csae.browser('chrome')
    csae.address()
